/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-12
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "app.h"
#include "prf.h"
#include "attm.h"
#include "ke_mem.h"
#include "ke_task.h"
#include "ke_timer.h"
#include "co_utils.h"
#include "prf_utils.h"
#include "rwip_task.h"
#include "rwip_config.h"
#include "user_config.h"
#include "ble_app_util.h"
#include "user_protocol.h"
#include "sw_timer_task.h"
#include "sys_task.h"
#include "app_motor.h"
#include "app_reset.h"
#include "hal_utc.h"

/*----------------------------macro file---------------------------*/
#define UTC_UPDATE_INTV_MS			(60000)	//60s

/*----------------------------type define--------------------------*/
enum sw_timer_state_t
{
    SW_TIMER_INIT,
	SW_TIMER_STATE_MAX
};

/*-------------------------func declaration------------------------*/
extern const struct ke_state_handler sw_timer_default_handler;

extern void app_button_timeout_handle(void);
extern void app_button_click_timeout_handle(void);
extern void app_skip_alg_max_inv_timeout_handle(void);
extern void app_skip_alg_signal_timeout_handle(void);
extern void ui_tick_4hz_cnt_handle(void);
extern void app_batt_mea_timer_timeout_handler(void);
extern void app_batt_charge_status_check_handle(void);

/*----------------------------var define---------------------------*/
static bool sw_timer_task_init_done = false;
ke_state_t sw_timer_state[1];
static const struct ke_task_desc TASK_DESC_TIMER = {NULL, &sw_timer_default_handler,
                                                  sw_timer_state, SW_TIMER_STATE_MAX, 1};

/*-----------------------------------------------------------------*/

static void sw_timer_start(ke_msg_id_t const timer_id, uint32_t ms)
{
	if(false == sw_timer_task_init_done)
		return;
	
	ke_timer_set(timer_id, TASK_SW_TIEMR, ms/10);
}

static void sw_timer_stop(ke_msg_id_t const timer_id)
{
	if(false == sw_timer_task_init_done)
		return;
	
	ke_timer_clear(timer_id, TASK_SW_TIEMR);
}
												  
void app_sw_timer_task_create(void)
{
	// Create APP task
	ke_task_create(TASK_SW_TIEMR, &TASK_DESC_TIMER);
	sw_timer_task_init_done = true;
}

/*void app_sw_timer_stop_all(void)
{
	for(int i=SW_TIMER_EVT_UTC;i<SW_TIMER_EVT_MAX;i++) {
		ke_timer_clear(i, TASK_SW_TIEMR);
	}
}*/

//UTC
void utc_timer_start(void)
{
	sw_timer_start(SW_TIMER_EVT_UTC, UTC_UPDATE_INTV_MS);
}

void utc_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_UTC);
}


int utc_timer_handle(void)
{
	hal_utc_update();

#if(UART_PRINTF_EN)
	//for test
	extern void utc_get_time(void);
	utc_get_time();
#endif
	
	sw_timer_start(SW_TIMER_EVT_UTC, UTC_UPDATE_INTV_MS);

	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}


//Button
void button_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_BUTTON, ms);
}

void button_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_BUTTON);
}


int button_timer_handle(void)
{
	app_button_timeout_handle();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}

void couple_click_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_DOUBLE_CLICK, ms);
}

void couple_click_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_DOUBLE_CLICK);
}

int couple_click_timer_handle(void)
{
	app_button_click_timeout_handle();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}



//Skip algorithm
void skip_alg_max_inv_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_CONTI_SKIP_MAX_INV, ms);
}

/*void skip_alg_max_inv_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_CONTI_SKIP_MAX_INV);
}*/

int skip_alg_max_inv_timer_handle(void)
{
	app_skip_alg_max_inv_timeout_handle();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}

////
void skip_alg_signal_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_ALG_SKIP_SIGNAL, ms);
}

/*void skip_alg_signal_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_ALG_SKIP_SIGNAL);
}*/

int skip_alg_signal_timer_handle(void)
{
	app_skip_alg_signal_timeout_handle();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}


//Disp update
static uint16_t disp_timer_inv_ms = 0;
void disp_auto_update_timer_start(uint32_t ms)
{
	disp_timer_inv_ms = ms;
	sw_timer_start(SW_TIMER_EVT_DISP_AUTO_UPDATE, disp_timer_inv_ms);
}

void disp_auto_update_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_DISP_AUTO_UPDATE);
}

int disp_auto_update_timer_handle(void)
{
    ui_tick_4hz_cnt_handle();
    sw_timer_start(SW_TIMER_EVT_DISP_AUTO_UPDATE, disp_timer_inv_ms);
    return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}


//BATT ADC
void batt_adc_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_BATT_ADC_READ, ms);
}

void batt_adc_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_BATT_ADC_READ);
}

int batt_adc_timer_handle(void)
{
	app_batt_mea_timer_timeout_handler();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}


//BATT Charge status check
void batt_chg_sta_chk_timer_start(uint32_t ms)
{
#if(BATT_CHARGE_ENABLE)
	sw_timer_start(SW_TIMER_EVT_BATT_CHG_CHK, ms);
#endif
}


int batt_chg_sta_ch_timer_handle(void)
{
#if(BATT_CHARGE_ENABLE)
	app_batt_charge_status_check_handle();
#endif
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}


//MOTOR
void motor_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_MOTOR_STOP, ms);
}

void motor_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_MOTOR_STOP);
}

int motor_timer_handle(void)
{
	app_motor_stop();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}


//BLE change slow adv
/*void ble_change_slow_adv_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_BLE_CHANGE_ADV, ms);
}

int ble_change_slow_adv_timer_handle(void)
{
	appm_stop_advertising();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}*/


//BLE resend 
void ble_resend_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_BLE_RESEND, ms);
}

void ble_resend_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_BLE_RESEND);
}

int ble_resend_timer_handle(void)
{
	app_ble_upload_resend_handle();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}


//BLE delay send 
void ble_delay_send_timer_start(uint32_t ms)
{
	sw_timer_start(SW_TIMER_EVT_BLE_DELAY_SEND, ms);
}

void ble_delay_send_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_BLE_DELAY_SEND);
}

int ble_delay_send_timer_handle(void)
{
	app_skip_mem_delay_send_timeout_handle();
	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}

//Delay reset
static bool ship_reset = false;
void delay_reset_timer_start(uint32_t ms, bool is_ship_reset)
{
	ship_reset = is_ship_reset;
	sw_timer_start(SW_TIMER_EVT_DELAY_RESET, ms);
}

/*void delay_reset_timer_stop(void)
{
	sw_timer_stop(SW_TIMER_EVT_DELAY_RESET);
}*/

int delay_reset_timer_handle(void)
{
	if(ship_reset) {
		sys_task_evt_set(SYS_EVT_SHIP_MODE_RESET);
	}
	else {
		app_system_reset();
	}

	return (KE_MSG_CONSUMED);	//Must need this, or it may make system crash.
}


/*
 * GLOBAL VARIABLE DEFINITIONS
 ****************************************************************************************
 */

/// Default State handlers definition
const struct ke_msg_handler sw_timer_default_state[] =
{
	{SW_TIMER_EVT_UTC,                (ke_msg_func_t) utc_timer_handle},
	{SW_TIMER_EVT_BUTTON,             (ke_msg_func_t) button_timer_handle},
	{SW_TIMER_EVT_DOUBLE_CLICK,       (ke_msg_func_t) couple_click_timer_handle},
	{SW_TIMER_EVT_CONTI_SKIP_MAX_INV, (ke_msg_func_t) skip_alg_max_inv_timer_handle},
    {SW_TIMER_EVT_ALG_SKIP_SIGNAL,    (ke_msg_func_t) skip_alg_signal_timer_handle},
	{SW_TIMER_EVT_DISP_AUTO_UPDATE,   (ke_msg_func_t) disp_auto_update_timer_handle},
	{SW_TIMER_EVT_BATT_ADC_READ,      (ke_msg_func_t) batt_adc_timer_handle},
	{SW_TIMER_EVT_BATT_CHG_CHK,       (ke_msg_func_t) batt_chg_sta_ch_timer_handle},
	{SW_TIMER_EVT_MOTOR_STOP,         (ke_msg_func_t) motor_timer_handle},
	//{SW_TIMER_EVT_BLE_CHANGE_ADV,     (ke_msg_func_t) ble_change_slow_adv_timer_handle},
	{SW_TIMER_EVT_BLE_RESEND,         (ke_msg_func_t) ble_resend_timer_handle},
	{SW_TIMER_EVT_BLE_DELAY_SEND,     (ke_msg_func_t) ble_delay_send_timer_handle},
	{SW_TIMER_EVT_DELAY_RESET,        (ke_msg_func_t) delay_reset_timer_handle},
};

/// Specifies the message handlers that are common to all states.
const struct ke_state_handler sw_timer_default_handler = KE_STATE_HANDLER(sw_timer_default_state);

